package com.infodms.dms.actions.sales.planmanage.RequirementForecast;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.actions.sales.planmanage.PlanUtil.PlanParaBean;
import com.infodms.dms.actions.sales.planmanage.PlanUtil.PlanUtil;
import com.infodms.dms.actions.sysmng.usemng.SgmDealerSysUser;
import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.sales.planmanage.OemRequireForecastReportDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TmVhclMaterialGroupPO;
import com.infodms.dms.po.TtVsMonthlyForecastDetailPO;
import com.infodms.dms.po.TtVsMonthlyForecastPO;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.csv.CsvWriterUtil;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.mvc.context.ResponseWrapper;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

import flex.messaging.io.ArrayList;

public class RequireForecastManage {

	public Logger logger = Logger.getLogger(SgmDealerSysUser.class);
	private final String requireForecastReportInitUrl = "/jsp/sales/planmanage/requireforecast/requireforecastreport.jsp";
	private final String requireForecastReportOutDateUrl = "/jsp/sales/planmanage/requireforecast/requireforecastreportoutdate.jsp";
	private final String requireForecastReportOpeUrl = "/jsp/sales/planmanage/requireforecast/requireforecastreportope.jsp";
	private final String requireForecastReportComUrl = "/jsp/sales/planmanage/requireforecast/requireforecastreportcomplete.jsp";
	private final String unReportRequireForecastReportComUrl = "/jsp/sales/planmanage/requireforecast/requireforecastunreportdealers.jsp";
	private final String lessReportRequireForecastReportComUrl = "/jsp/sales/planmanage/requireforecast/requireforecastlessmonthplandealers.jsp";
	private final String requireForecastHistoryInitUrl = "/jsp/sales/planmanage/requireforecast/requireforecasthistoryshow.jsp";
	
	public void requireForecastReportInit(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			
			 PlanParaBean bean=PlanUtil.selectForecastParas();
			 int curDay=PlanUtil.getCurrentDay();
			 //如果是区域用户登陆并且已经超过规定预测日期，不能进行预测
			 if(logonUser.getDutyType().equals(Constant.DUTY_TYPE_LARGEREGION.toString())){
				 if(curDay>bean.getSbuForecastEndDay()||curDay<bean.getSbuForecastStartDay()){
					 act.setForword(requireForecastReportOutDateUrl);
				 }else{
					 List<Map<String, Object>> areaBusList=MaterialGroupManagerDao.getPoseIdBusiness(logonUser.getPoseId().toString());
					 List<Map<String, Object>> mapList=getForecastDate(bean);
					 act.setOutData("mapList", mapList);
					 act.setOutData("areaBusList", areaBusList);
					 act.setForword(requireForecastReportInitUrl);
				 }
			 }else{
				 List<Map<String, Object>> areaBusList=MaterialGroupManagerDao.getPoseIdBusiness(logonUser.getPoseId().toString());
				 List<Map<String, Object>> mapList=getForecastDate(bean);
				 act.setOutData("mapList", mapList);
				 act.setOutData("areaBusList", areaBusList);
				 act.setForword(requireForecastReportInitUrl);
			 }
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/*
	 * 取得查询日期，展示月数，提前预测月数，0表示从当前月开始预测，每次预测月数（开始预测月+每次预测月数）
	 */
	private List<Map<String, Object>> getForecastDate(PlanParaBean bean){
		List<Map<String, Object>> list=new ArrayList();
		Map<String, Object> map=new HashMap<String, Object>();
		int his=bean.getShowHistoryMonthAmt();//展示月数，
		int start=-his+bean.getForecastPreMonth();
		int end=bean.getForecastPreMonth()+bean.getForecastMonthAmt();//每次预测月数（开始预测月+每次预测月数）
		for(int i=start;i<end;i++){
			list.add(PlanUtil.getMapDate(i));
		}
		return list;
	}
	/*
	 * 取得查询日期，展示月数，提前预测月数，0表示从当前月开始预测，每次预测月数（开始预测月+每次预测月数）
	 */
	private List<Map<String, Object>> getForecastDateAmt(PlanParaBean bean){
		List<Map<String, Object>> list=new ArrayList();
		Map<String, Object> map=new HashMap<String, Object>();
		for(int i=bean.getForecastPreMonth();i<bean.getForecastMonthAmt()+bean.getForecastPreMonth();i++){
			list.add(PlanUtil.getMapDate(i));
		}
		return list;
	}
	public void requireForecastReportModelSearch(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			//如果是部门，查询用ORG_ID是PARENT_ORG_ID，这里判断是不是大区，即经销商的上级
			 String duty=logonUser.getDutyType();
			 String orgLevel="";
			 String orgId=logonUser.getOrgId().toString();
			 String porgId=logonUser.getParentOrgId();
			 if(null==duty||"".equals(duty)){
				 duty="0";
			 }
			 if(new Integer(duty).intValue()==Constant.DUTY_TYPE_LARGEREGION.intValue()){
				 orgLevel="sub";
			 }
			 if(new Integer(duty).intValue()==Constant.DUTY_TYPE_DEPT.intValue()){
				 orgLevel="dept";
			 }
			
			 RequestWrapper request=act.getRequest();
			 String areaId=request.getParamValue("areaId");
			 areaId=areaId.substring(0,areaId.length()-1);
			 PlanParaBean bean=PlanUtil.selectForecastParas();
			 if(null==bean){
				 throw new BizException(act, ErrorCodeConstant.INVALIDATION_DATA, "系统参数错误");
			 }
			 
			 List<Map<String, Object>> mapList=getForecastDate(bean);
			 
			 //目前单月预测，校验如果存在已经上报数据，则不返回数据
			 List<Map<String, Object>> cList=getForecastDateAmt(bean);
			 Map<String, Object> map=cList.get(0);
			 String year=(String)map.get("YEAR");
			 String month=(String)map.get("MONTH");
			 TtVsMonthlyForecastPO conPo=new TtVsMonthlyForecastPO();
			 conPo.setAreaId(new Long(areaId));
			 conPo.setOrgId(new Long(orgId));
			 conPo.setForecastYear(new Integer(year));
			 conPo.setForecastMonth(new Integer(month));
			 conPo.setOrgType(Constant.ORG_TYPE_OEM);
			 conPo.setCompanyId(logonUser.getCompanyId());
			 conPo.setStatus(Constant.FORECAST_STATUS_CONFIRM);
			 //校验用LIST，SIZE>0则不能操作,如果预测第一个月已确认，那么不能操作
			 List<PO> checkList=dao.select(conPo);
			 List<Map<String, Object>> infoList=null;
			 if(null==checkList||checkList.size()>0){
				 
			 }else{
				 infoList=dao.selectOemRequireForecastModelInfo(porgId,orgId, areaId, mapList, orgLevel,logonUser.getCompanyId().toString());
			 }
		     act.setOutData("areaId", areaId);
		     act.setOutData("mapList", mapList);
		     act.setOutData("infoList",infoList );
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/*
	 * 预测查询
	 * 需要查询参数，确定预测时间浮动
	 */
	public void requireForecastSearch(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			
			//如果是部门，查询用ORG_ID是PARENT_ORG_ID，这里判断是不是大区，即经销商的上级
			 String duty=logonUser.getDutyType();
			 String orgLevel="";
			 String orgId=logonUser.getOrgId().toString();
			 String porgId=logonUser.getParentOrgId();
			 if(null==duty||"".equals(duty)){
				 duty="0";
			 }
			 if(new Integer(duty).intValue()==Constant.DUTY_TYPE_LARGEREGION.intValue()){
				 orgLevel="sub";
			 }
			 if(new Integer(duty).intValue()==Constant.DUTY_TYPE_DEPT.intValue()){
				 orgLevel="dept";
			 }
			 
			 RequestWrapper request=act.getRequest();
			 String areaId=request.getParamValue("area");
			 String modelId=request.getParamValue("modelId");
			 
			 PlanParaBean bean=PlanUtil.selectForecastParas();
			 List<Map<String, Object>> dateList = getForecastDateAmt(bean);
			
			 TmVhclMaterialGroupPO gconPo=new TmVhclMaterialGroupPO();
			 gconPo.setGroupId(new Long(modelId));
			 List<PO> glist=dao.select(gconPo);
			 if(null==glist||glist.size()==0){
				 throw new Exception("车型不存在");
			 }
			 TmVhclMaterialGroupPO gpo=(TmVhclMaterialGroupPO)glist.get(0);
			 String modelName=gpo.getGroupName();
			 List<Map<String, Object>> ps=null;
			 //这里要做判断，总部和区域要分开SQL查询
			 ps=dao.selectOemForecastOpeList(porgId,modelId,orgId, areaId, dateList,orgLevel,logonUser.getCompanyId().toString());
			 act.setOutData("ps", ps);
			 act.setOutData("dateList", dateList); //预测月份
			 act.setOutData("modelName", modelName);//预测车型
			 act.setOutData("areaId", areaId);
			 act.setForword(requireForecastReportOpeUrl);
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	/*
	 * 保存需求预测
	 */
	public void requireForecastSave(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			 RequestWrapper request=act.getRequest();
			 String areaId=request.getParamValue("areaId");
			 String orgId=logonUser.getOrgId().toString();
			 
			 Enumeration<String> params=request.getParamNames();
			 boolean bl=true;//以防万一，没啥用，如果第一月已经确认，那不可操作，这是用来确定 是否是第一个月，如果是，查询未确认状态的，否则查所有清掉
			 while (params.hasMoreElements()) {
				String elem = (String) params.nextElement();
				if(elem.indexOf("ipt")!=-1){	
					String amt=request.getParamValue(elem);
					if(null==amt){
						amt="0";
					}
					elem=elem.substring(3,elem.length());
					String[] gym=elem.split("_");
					String groupId=gym[0];
					String year=gym[1];
					String month=gym[2];
					
					List<Map<String, Object>> clrList_=dao.selectClrOemForecast(year, month, areaId, orgId,"",logonUser.getCompanyId().toString(), bl);
					List<Map<String, Object>> clrList=dao.selectClrOemForecast(year, month, areaId, orgId, groupId,logonUser.getCompanyId().toString(), bl);
					if(null!=clrList_&&clrList_.size()>0) {
						Long forecastId_ = new Long(clrList_.get(0).get("FORECAST_ID").toString());
						if (null!=clrList && clrList.size()>0) {
							Long detailId_ = new Long(clrList.get(0).get("DETAIL_ID").toString()) ;
							TtVsMonthlyForecastDetailPO detailPo_=new TtVsMonthlyForecastDetailPO();
							detailPo_.setDetailId(detailId_);
							TtVsMonthlyForecastDetailPO detailPo=new TtVsMonthlyForecastDetailPO();
							detailPo.setDetailId(detailId_) ;
							detailPo.setForecastAmount(new Integer(amt));
							
							dao.update(detailPo_, detailPo);
						} else {
							TtVsMonthlyForecastDetailPO detailPo=new TtVsMonthlyForecastDetailPO();
							detailPo.setDetailId(new Long(SequenceManager.getSequence("")));
							detailPo.setForecastId(forecastId_);
							detailPo.setGroupId(new Long(groupId));
							detailPo.setForecastAmount(new Integer(amt));
							detailPo.setCreateBy(logonUser.getUserId());
							detailPo.setCreateDate(new Date());
							
							dao.insert(detailPo);
						}						
					} else {
						//保存数据
						TtVsMonthlyForecastPO mainPo=new TtVsMonthlyForecastPO();
						Long mseq=new Long(SequenceManager.getSequence(""));
						mainPo.setForecastId(mseq);
						mainPo.setCompanyId(logonUser.getCompanyId());
						mainPo.setOrgId(new Long(orgId));
						mainPo.setAreaId(new Long(areaId));
						mainPo.setForecastYear(new Integer(year));
						mainPo.setForecastMonth(new Integer(month));
						mainPo.setOrgType(Constant.ORG_TYPE_OEM);
						mainPo.setStatus(Constant.FORECAST_STATUS_UNCONFIRM);
						mainPo.setCreateBy(logonUser.getUserId());
						mainPo.setCreateDate(new Date());
						
						dao.insert(mainPo);	
						
						TtVsMonthlyForecastDetailPO detailPo=new TtVsMonthlyForecastDetailPO();
						detailPo.setDetailId(new Long(SequenceManager.getSequence("")));
						detailPo.setForecastId(mseq);
						detailPo.setGroupId(new Long(groupId));
						detailPo.setForecastAmount(new Integer(amt));
						detailPo.setCreateBy(logonUser.getUserId());
						detailPo.setCreateDate(new Date());
						
						dao.insert(detailPo);
					}
					
					/*//清空 当前登陆经销商年、月、业务范围内、未提报车辆配置的需求预测,
					List<Map<String, Object>> clrList=dao.selectClrOemForecast(year, month, areaId, orgId, groupId,logonUser.getCompanyId().toString(), bl);
					bl=false;
					if(null!=clrList&&clrList.size()>0){
						for(int i=0;i<clrList.size();i++){
							Map<String, Object> map=clrList.get(i);
							Long forecastId=new Long(map.get("FORECAST_ID").toString());
							TtVsMonthlyForecastDetailPO clrDPo=new TtVsMonthlyForecastDetailPO();
							clrDPo.setForecastId(forecastId);
							TtVsMonthlyForecastPO clrPo=new TtVsMonthlyForecastPO();
							clrPo.setForecastId(forecastId);
							dao.delete(clrDPo);
							dao.delete(clrPo);
						}
					}
					
					TtVsMonthlyForecastPO mainPo=new TtVsMonthlyForecastPO();
					mseq=new Long(SequenceManager.getSequence(""));
					mainPo.setForecastId(mseq);
					mainPo.setCompanyId(logonUser.getCompanyId());
					mainPo.setOrgId(new Long(orgId));
					mainPo.setAreaId(new Long(areaId));
					mainPo.setForecastYear(new Integer(year));
					mainPo.setForecastMonth(new Integer(month));
					mainPo.setOrgType(Constant.ORG_TYPE_OEM);
					mainPo.setStatus(Constant.FORECAST_STATUS_UNCONFIRM);
					mainPo.setCreateBy(logonUser.getUserId());
					mainPo.setCreateDate(new Date());
					
					dao.insert(mainPo);	
					
					TtVsMonthlyForecastDetailPO detailPo=new TtVsMonthlyForecastDetailPO();
					detailPo.setDetailId(new Long(SequenceManager.getSequence("")));
					detailPo.setForecastId(mseq);
					detailPo.setGroupId(new Long(groupId));
					detailPo.setForecastAmount(new Integer(amt));
					detailPo.setCreateBy(logonUser.getUserId());
					detailPo.setCreateDate(new Date());
					
					dao.insert(detailPo);*/
				}else{
					continue;
				}
				
			}
			act.setOutData("areaId", areaId);
			act.setForword("/sales/planmanage/RequirementForecast/RequireForecastManage/requireForecastReportInit.do");
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	/*
	 * 区域上报，总部调整完成
	 */
	
	public void requireForecastReport(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			 PlanParaBean bean=PlanUtil.selectForecastParas();
			 List<Map<String, Object>> mapList=getForecastDate(bean);
			 RequestWrapper request=act.getRequest();
			 String areaId=request.getParamValue("area");
			 String orgId=logonUser.getOrgId().toString();
			 
			 for(int i=0;i<mapList.size();i++){
				 Map<String, Object> map=mapList.get(i);
				 String year=(String)map.get("YEAR");
				 String month=(String)map.get("MONTH");
				 
				 TtVsMonthlyForecastPO conPo=new TtVsMonthlyForecastPO();
				 conPo.setAreaId(new Long(areaId));
				 conPo.setOrgId(new Long(orgId));
				 conPo.setForecastYear(new Integer(year));
				 conPo.setForecastMonth(new Integer(month));
				 conPo.setStatus(Constant.FORECAST_STATUS_UNCONFIRM);
				 conPo.setOrgType(Constant.ORG_TYPE_OEM);
				 
				 TtVsMonthlyForecastPO valPo=new TtVsMonthlyForecastPO();
				 valPo.setStatus(Constant.FORECAST_STATUS_CONFIRM);
				 valPo.setUpdateBy(logonUser.getUserId());
				 valPo.setUpdateDate(new Date());
				 
				 dao.update(conPo,valPo);
			 }
			 act.setForword(requireForecastReportComUrl);
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/*
	 * 转发未提报组织名单URL
	 */
	public void unreportForwardAction(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			 RequestWrapper request=act.getRequest();
			 String areaId=request.getParamValue("area");
			 act.setOutData("area", areaId);
			 act.setForword(unReportRequireForecastReportComUrl);
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/*
	 * 未提报组织名单查询
	 */
	public void selectUnreportForecastDealers(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			 PlanParaBean bean=PlanUtil.selectForecastParas();
			 List<Map<String, Object>> mapList=getForecastDateAmt(bean);
			 RequestWrapper request=act.getRequest();
			 String areaId=request.getParamValue("area");
			 String orgId=logonUser.getOrgId().toString();
			 String parentOrgId=logonUser.getParentOrgId();
			 String duty=logonUser.getDutyType();
			 
			 PageResult<Map<String, Object>> ps=null;
			 // 处理当前页
			 Integer curPage = request.getParamValue("curPage") != null ?Integer.parseInt(request.getParamValue("curPage")): 1; 
			
			 if(new Integer(duty).intValue()==Constant.DUTY_TYPE_LARGEREGION.intValue()){
				 ps=dao.selectSubRequireForecastUnreportedDealers(orgId, areaId,logonUser.getCompanyId().toString(), mapList, curPage, 1000);
			 }else{
				 ps=dao.selectDeptRequireForecastUnreportedDealers(orgId, areaId, mapList, curPage, 1000);
			 }
			 act.setOutData("ps", ps);
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	/*
	 * 跳转至提报预测数量小于月度任务数量的经销商（车系）
	 */
	public void lessReportForwardAction(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			 RequestWrapper request=act.getRequest();
			 String areaId=request.getParamValue("area");
			 act.setOutData("area", areaId);
			 act.setForword(lessReportRequireForecastReportComUrl);
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/*
	 * 查询预测量小于目标量的组织名单
	 */
	public void selectLessForecastDealers(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			 PlanParaBean bean=PlanUtil.selectForecastParas();
			 List<Map<String, Object>> mapList=getForecastDateAmt(bean);
			 RequestWrapper request=act.getRequest();
			 String areaId=request.getParamValue("area");
			 String orgId=logonUser.getOrgId().toString();
			 String parentOrgId=logonUser.getParentOrgId();
			 String duty=logonUser.getDutyType();
			 
			 PageResult<Map<String, Object>> ps=null;
			 // 处理当前页
			 Integer curPage = request.getParamValue("curPage") != null ?Integer.parseInt(request.getParamValue("curPage")): 1; 
			
			 if(new Integer(duty).intValue()==Constant.DUTY_TYPE_LARGEREGION.intValue()){
				 ps=dao.selectSubForecastLessThanMonthPlanDealers(orgId, areaId, mapList, curPage, 1000);
			 }else{
				 ps=dao.selectDeptForecastLessThanMonthPlanDealers(parentOrgId, areaId, mapList, curPage, 1000);
			 }
			 act.setOutData("ps", ps);
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/*
	 * 未提报组织预测下载
	 */
	public void oemRequireForecastUnreportExport() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		OutputStream os = null;
		try {
			 RequestWrapper request = act.getRequest();
			 ResponseWrapper response = act.getResponse();
			 PlanParaBean bean=PlanUtil.selectForecastParas();
			 List<Map<String, Object>> mapList=getForecastDateAmt(bean);
			 String areaId=request.getParamValue("area");
			 String orgId=logonUser.getOrgId().toString();
			 String parentOrgId=logonUser.getParentOrgId();
			 String duty=logonUser.getDutyType();
			// 导出的文件名
			String fileName = "需求预测.csv";
			// 导出的文字编码
			fileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
			response.setContentType("Application/text/csv");
			response.addHeader("Content-Disposition", "attachment;filename="
					+ fileName);
			List<List<Object>> list=null;
			 if(new Integer(duty).intValue()==Constant.DUTY_TYPE_LARGEREGION.intValue()){
				 list=getSubUnReortDealerList(orgId, areaId,logonUser.getCompanyId().toString(), mapList);
			 }else{
				 list=getDeptUnReortDealerList(orgId, areaId, mapList);
			 }
			os = response.getOutputStream();
			CsvWriterUtil.writeCsv(list, os);
			os.flush();
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "最大配额总量查询");
			logger.error(logonUser, e1);
			act.setException(e1);
		} finally {
			if (null != os) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/*
	 * 区域未提报组织下载
	 */
	private List<List<Object>> getSubUnReortDealerList(String orgId, String areaId,String companyId, List<Map<String, Object>> mapList){
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		List<List<Object>> list = new LinkedList<List<Object>>();

		List<Object> listTemp = new LinkedList<Object>();
		listTemp.add("大区名称");
		listTemp.add("经销商代码");
		listTemp.add("经销商名称");
		listTemp.add("预测月份");
		listTemp.add("联系人");
		listTemp.add("联系方式");
		list.add(listTemp);

		List<Map<String, Object>> results =dao.downLoadSubRequireForecastUnreportedDealers(orgId, areaId,companyId, mapList);
		
		for (int i = 0; i < results.size(); i++) {
			Map<String, Object> record = results.get(i);
			listTemp = new LinkedList<Object>();
			listTemp.add(CommonUtils.checkNull(record.get("ORG_NAME")));
			listTemp.add(CommonUtils.checkNull(record.get("DEALER_CODE")));
			listTemp.add(CommonUtils.checkNull(record.get("DEALER_SHORTNAME")));
			listTemp.add(CommonUtils.checkNull(record.get("FORECAST_MONTH")));
			listTemp.add(CommonUtils.checkNull(record.get("LINK_MAN"))+"");
			listTemp.add(CommonUtils.checkNull(record.get("PHONE")));
			
			list.add(listTemp);
		}
		return list;
	}
	/*
	 * 总部查未提报组织下载
	 */
	private List<List<Object>> getDeptUnReortDealerList(String parentOrgId, String areaId, List<Map<String, Object>> mapList){
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		List<List<Object>> list = new LinkedList<List<Object>>();

		List<Object> listTemp = new LinkedList<Object>();
		listTemp.add("大区名称");
		listTemp.add("经销商代码");
		listTemp.add("经销商名称");
		listTemp.add("预测月份");
		listTemp.add("联系人");
		listTemp.add("联系方式");
		list.add(listTemp);

		List<Map<String, Object>> results =dao.downLoadDeptRequireForecastUnreportedDealers(parentOrgId, areaId, mapList);
		
		for (int i = 0; i < results.size(); i++) {
			Map<String, Object> record = results.get(i);
			listTemp = new LinkedList<Object>();
			listTemp.add(CommonUtils.checkNull(record.get("ORG_NAME")));
			listTemp.add(CommonUtils.checkNull(record.get("DEALER_CODE")));
			listTemp.add(CommonUtils.checkNull(record.get("DEALER_SHORTNAME")));
			listTemp.add(CommonUtils.checkNull(record.get("FORECAST_MONTH")));
			listTemp.add(CommonUtils.checkNull(record.get("LINK_MAN"))+"");
			listTemp.add(CommonUtils.checkNull(record.get("PHONE")));
			list.add(listTemp);
		}
		return list;
	}
	
	/*
	 * 预测数量小月度目标预测下载
	 */
	public void oemRequireForecastLessExport() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		OutputStream os = null;
		try {
			 RequestWrapper request = act.getRequest();
			 ResponseWrapper response = act.getResponse();
			 PlanParaBean bean=PlanUtil.selectForecastParas();
			 List<Map<String, Object>> mapList=getForecastDateAmt(bean);
			 String areaId=request.getParamValue("area");
			 String orgId=logonUser.getOrgId().toString();
			 String parentOrgId=logonUser.getParentOrgId();
			 String duty=logonUser.getDutyType();
			// 导出的文件名
			String fileName = "需求预测.csv";
			// 导出的文字编码
			fileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
			response.setContentType("Application/text/csv");
			response.addHeader("Content-Disposition", "attachment;filename="
					+ fileName);
			List<List<Object>> list=null;
			 if(new Integer(duty).intValue()==Constant.DUTY_TYPE_LARGEREGION.intValue()){
				 list=getSubLessReortDealerList(orgId, areaId, mapList);
			 }else{
				 list=getDeptLessReortDealerList(parentOrgId, areaId, mapList);
			 }
			os = response.getOutputStream();
			CsvWriterUtil.writeCsv(list, os);
			os.flush();
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "最大配额总量查询");
			logger.error(logonUser, e1);
			act.setException(e1);
		} finally {
			if (null != os) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/*
	 * 区域预测数量小月度目标
	 */
	private List<List<Object>> getSubLessReortDealerList(String orgId, String areaId, List<Map<String, Object>> mapList){
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		List<List<Object>> list = new LinkedList<List<Object>>();

		List<Object> listTemp = new LinkedList<Object>();
		listTemp.add("经销商代码");
		listTemp.add("经销商名称");
		listTemp.add("预测月份");
		listTemp.add("联系人");
		listTemp.add("联系方式");
		listTemp.add("预测数量");
		listTemp.add("目标数量");
		list.add(listTemp);

		List<Map<String, Object>> results =dao.downLoadSubForecastLessThanMonthPlanDealers(orgId, areaId, mapList);
		
		for (int i = 0; i < results.size(); i++) {
			Map<String, Object> record = results.get(i);
			listTemp = new LinkedList<Object>();
			listTemp.add(CommonUtils.checkNull(record.get("DEALER_CODE")));
			listTemp.add(CommonUtils.checkNull(record.get("DEALER_SHORTNAME")));
			listTemp.add(CommonUtils.checkNull(record.get("FORECAST_MONTH")));
			listTemp.add(CommonUtils.checkNull(record.get("LINK_MAN"))+"");
			listTemp.add(CommonUtils.checkNull(record.get("PHONE")));
			listTemp.add(CommonUtils.checkNull(record.get("AMOUNT"))+"");
			listTemp.add(CommonUtils.checkNull(record.get("SALE_AMOUNT")));
			list.add(listTemp);
		}
		return list;
	}
	/*
	 * 总部查预测数量小月度目标下载
	 */
	private List<List<Object>> getDeptLessReortDealerList(String parentOrgId, String areaId, List<Map<String, Object>> mapList){
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		List<List<Object>> list = new LinkedList<List<Object>>();

		List<Object> listTemp = new LinkedList<Object>();
		listTemp.add("经销商代码");
		listTemp.add("经销商名称");
		listTemp.add("预测月份");
		listTemp.add("联系人");
		listTemp.add("联系方式");
		listTemp.add("预测数量");
		listTemp.add("目标数量");
		list.add(listTemp);

		List<Map<String, Object>> results =dao.downLoadDeptForecastLessThanMonthPlanDealers(parentOrgId, areaId, mapList);
		
		for (int i = 0; i < results.size(); i++) {
			Map<String, Object> record = results.get(i);
			listTemp = new LinkedList<Object>();
			listTemp.add(CommonUtils.checkNull(record.get("DEALER_CODE")));
			listTemp.add(CommonUtils.checkNull(record.get("DEALER_SHORTNAME")));
			listTemp.add(CommonUtils.checkNull(record.get("FORECAST_MONTH")));
			listTemp.add(CommonUtils.checkNull(record.get("LINK_MAN"))+"");
			listTemp.add(CommonUtils.checkNull(record.get("PHONE")));
			listTemp.add(CommonUtils.checkNull(record.get("AMOUNT"))+"");
			listTemp.add(CommonUtils.checkNull(record.get("SALE_AMOUNT")));
			list.add(listTemp);
		}
		return list;
	}
	
	/*
	 * 打开参考信息查看页
	 */
	public void requireForecastHistoryInit(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request=act.getRequest();
			String groupId=request.getParamValue("groupId")==null?"":request.getParamValue("groupId");
			act.setOutData("groupId", groupId);
			act.setForword(requireForecastHistoryInitUrl);
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	
	/*
	 * 参考信息查询
	 */
	public void forecastReportThreeMonthHistory(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		OemRequireForecastReportDao dao=OemRequireForecastReportDao.getInstance();
		try {
			 RequestWrapper request=act.getRequest();
			 String groupId=request.getParamValue("groupId")==null?"":request.getParamValue("groupId");
			 
			 List<Map<String, Object>> list=new ArrayList();
			 for(int i=-3;i<0;i++){
				 list.add(PlanUtil.getMapDate(1));
			 }
			 
			 PageResult<Map<String, Object>> ps=null;
			 // 处理当前页
			 Integer curPage = request.getParamValue("curPage") != null ?Integer.parseInt(request.getParamValue("curPage")): 1; 
			 
			 ps=dao.selectHistoryMonth(logonUser.getDutyType(),logonUser.getOrgId().toString(), groupId, list,Constant.PAGE_SIZE, curPage);
			 
			 act.setOutData("ps",ps);
			 
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"未知错误!");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
}
